 //
//  HXJobRecordTableViewController.m
//  KD
//
//  Created by ifly on 16/4/20.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXJobRecordTableViewController.h"
#import "HXJobWeekDetailsViewController.h"
//#import "HXPictureGatherViewController.h"
#import "HXNowJobWeekViewController.h"
#import "HXSitePictureViewController.h"

@interface HXJobRecordTableViewController ()
@property(nonatomic,strong) NSArray *weekArray;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,assign) NSInteger week;
@property(nonatomic,strong) NSMutableArray<HXJopRecordListModel *> *nowDataArray;
@property(nonatomic,strong) NSMutableArray<HXJopRecordListModel *> *dataArray;
@property(nonatomic,strong) UIView *nowView;
@property(nonatomic,strong) NSArray<HXNowJobWeekViewController *> *nowArray;
@property(nonatomic,strong) UIView *otherView;
@property(nonatomic,strong) NSArray<HXJobWeekDetailsViewController *> *otherArray;

@end

@implementation HXJobRecordTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self addGesture];
    self.week = 0;//当前周
    self.weekArray = [[NSArray alloc] initWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",nil];
    
    [self firstRefreshNowWeekData];
    self.title = @"工作周报";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
}

/**添加手势左划下一周 右划上一周*/
-(void)addGesture{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]init];
    [self.view addGestureRecognizer:swipeLeft];
    [swipeLeft addTarget:self action:@selector(swipeLeftClick)];
    swipeLeft.direction =UISwipeGestureRecognizerDirectionLeft;
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]init];
    [self.view addGestureRecognizer:swipeRight];
    [swipeRight addTarget:self action:@selector(swipeRightClick)];
    swipeRight.direction =UISwipeGestureRecognizerDirectionRight;
}


-(void)swipeLeftClick{//下一周
    if (self.week == 0) {//已经是当前周
         [SVProgressHUD showErrorWithStatus:@"已是当前周"];
        return;
    }
    self.week--;
    if (self.week == 0) {//下一周是当前周
        [SVProgressHUD showErrorWithStatus:@"已是当前周"];
        [self refreshNowWeekData];
        return;
    }else{
        [self refreshLeftWeekData];
    }
}

-(void)swipeRightClick{//上一周
    self.week++;
    [self refreshRightWeekData];
}

-(void)firstRefreshNowWeekData{
    [SVProgressHUD showWithStatus:@"正在加载" ];
    [NetworkAPI getJobRecordListWithUserId:[UserDefaultHandler getUserDetault:kID]
                                   GroupID:@"-1"
                                    WeekID:@"0"
                                   success:^(HXJopRecordModel *result) {
                                       [SVProgressHUD dismiss];
                                       NSLog(@"[UserDefaultHandler getUserDetault:kID]-------%@",[UserDefaultHandler getUserDetault:kID]);
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD showSuccessWithStatus:@"加载成功"];
                                           /**开始处理数据*/
                                           [self.nowDataArray removeAllObjects];

                                           NSInteger week = self.week == 0?[self nowWeek]:7;//今天周几
                                           
                                           BOOL isAdd = NO;  // week = 7 周日 6,0,1,2,3,4,5
                                           for (int i = 0; i<week; i++) {//今天周几就创建几个
                                               isAdd = NO;
                                               for (HXJopRecordListModel *model in result.resultlist) {
                                                   /**有这天的数据 就添加
                                                    但是也有可能今天只有上午有 下午的Work_PMText是 -1 */
                                                   if ([model.Week isEqualToString:self.weekArray[i]]) {
                                                       model.isMy = @"no";//不是自己创建的
                                                       [self.nowDataArray addObject:model];
                                                       [result.resultlist removeObject:model];
                                                       isAdd = YES;
                                                       break;
                                                   }
                                               }
                                               if (isAdd) continue;//添加了这天数据就跳出
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];
                                               
                                               NSMutableArray *array;
                                               
                                               if (self.week == 0) {
                                                   
                                                   array = [self nowWeekDate];
                                                   myModel.CreateDate = array[i];//告诉日期
                                               }else{
                                               
                                                   array = [self lastWeekDate];
                                                   
                                                   myModel.CreateDate = array[i];//告诉日期
                                               }

                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.nowDataArray addObject:myModel];
                                           }
                                           [self addNowData];
                                       }else if ([result.code isEqualToString:@"400"]){//没数据 每天都没有
                                           [self.nowDataArray removeAllObjects];
                                           NSInteger week = self.week == 1?7:[self nowWeek];//今天周几
                                           for (int i = 0; i<week; i++) {//今天周几就创建几个
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是星期几
                                               
                                               NSMutableArray *array;
                                               
                                               if (self.week == 0) {
                                                   
                                                   array = [self nowWeekDate];
                                                   myModel.CreateDate = array[i];//告诉日期
                                               }else{
                                                   
                                                   array = [self lastWeekDate];
                                                   
                                                   myModel.CreateDate = array[i];//告诉日期
                                               }
                                               
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.nowDataArray addObject:myModel];
                                           }
                                           [self addNowData];
                                       }else{
                                           //                                           self.week++;
                                           [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                           //                                           return @"no";
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       //                                       self.week++;
                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                       //                                       return @"no";
                                   }];
}

-(void)refreshNowWeekData{
    [SVProgressHUD showWithStatus:@"正在加载" ];
    [NetworkAPI getJobRecordListWithUserId:[UserDefaultHandler getUserDetault:kID]
                                   GroupID:@"-1"
                                    WeekID:@"0"
                                   success:^(HXJopRecordModel *result) {
                                       [SVProgressHUD dismiss];
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD showSuccessWithStatus:@"加载成功"];
                                           /**开始处理数据*/
                                           [self.nowDataArray removeAllObjects];
                                           
                                           NSInteger week = self.week == 0?[self nowWeek]:7;//今天周几
                                           
                                           BOOL isAdd = NO;
                                           for (int i = 0; i<week; i++) {//今天周几就创建几个
                                               isAdd = NO;
                                               for (HXJopRecordListModel *model in result.resultlist) {
                                                   /**有这天的数据 就添加
                                                    但是也有可能今天只有上午有 下午的Work_PMText是 -1 */
                                                   if ([model.Week isEqualToString:self.weekArray[i]]) {
                                                       model.isMy = @"no";//不是自己创建的
                                                       [self.nowDataArray addObject:model];
                                                       [result.resultlist removeObject:model];
                                                       isAdd = YES;
                                                       break;
                                                   }
                                               }
                                               if (isAdd) continue;//添加了这天数据就跳出
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];
                                               
                                               if (self.week == 0) {
                                                   
                                                   NSMutableArray *array = [self nowWeekDate];
                                                   myModel.CreateDate = array[i];
                                                   
                                               }else{
                                               
                                                   NSMutableArray *array = [self lastWeekDate];
                                                   myModel.CreateDate = array[i];
                                               }
                                               
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.nowDataArray addObject:myModel];
                                           }
                                           [self addNowData];
                                       }else if ([result.code isEqualToString:@"400"]){//没数据 每天都没有
                                            [self.nowDataArray removeAllObjects];
                                           NSInteger week = [self nowWeek];//今天周几
                                           for (int i = 0; i<week; i++) {//今天周几就创建几个
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是星期几
                                               NSMutableArray *array = [self nowWeekDate];
                                               myModel.CreateDate = array[i];//告诉日期
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.nowDataArray addObject:myModel];
                                           }
                                           [self addNowData];
                                       }else{
                                           self.week++;
                                           [SVProgressHUD showErrorWithStatus:@"网络错误"];
//                                           return @"no";
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       self.week++;
                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
//                                       return @"no";
                                   }];
}

/**非本周数据*/
-(void)refreshRightWeekData{//上一周
    [SVProgressHUD showWithStatus:@"正在加载" ];
    [NetworkAPI getJobRecordListWithUserId:[UserDefaultHandler getUserDetault:kID]
                                   GroupID:@"-1"
                                    WeekID:[NSString stringWithFormat:@"%ld",(long)self.week]
                                   success:^(HXJopRecordModel *result) {
                                       [SVProgressHUD dismiss];
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD showSuccessWithStatus:@"加载成功"];
                                           [self.dataArray removeAllObjects];
                                           BOOL isAdd = NO;
                                           for (int i = 0; i<7; i++) {//之前周肯定是七个
                                               isAdd = NO;
                                               for (HXJopRecordListModel *model in result.resultlist) {
                                                   if ([model.Week isEqualToString:self.weekArray[i]]) {//有这天的数据 就添加
                                                       model.isMy = @"no";//不是自己创建的
                                                       [self.dataArray addObject:model];
                                                       [result.resultlist removeObject:model];
                                                       isAdd = YES;
                                                       break;
                                                   }
                                               }
                                               if (isAdd) continue;//添加了这天数据就跳出
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是周几
                                               NSMutableArray *array = [self lastWeekDate];
                                               myModel.CreateDate = array[i];
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.dataArray addObject:myModel];
                                           }
                                           [self addOtherData];
                                       }else if([result.code isEqualToString:@"400"]){
                                            [self.dataArray removeAllObjects];
                                           for (int i = 0; i<7; i++) {//之前周肯定是七个
                                               
                                              HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是周几
                                               NSMutableArray *array = [self lastWeekDate];
                                               myModel.CreateDate = array[i];
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.dataArray addObject:myModel];
                                           }
                                           [self addOtherData];
                                       }else{
                                           self.week--;
                                           [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       self.week--;
                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   }];
}

-(void)refreshLeftWeekData{//下一周
    [SVProgressHUD showWithStatus:@"正在加载" ];
    [NetworkAPI getJobRecordListWithUserId:[UserDefaultHandler getUserDetault:kID]
                                   GroupID:@"-1"
                                    WeekID:[NSString stringWithFormat:@"%ld",(long)self.week]
                                   success:^(HXJopRecordModel *result) {
                                       [SVProgressHUD dismiss];
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD showSuccessWithStatus:@"加载成功"];
                                           [self.dataArray removeAllObjects];
                                           BOOL isAdd = NO;
                                           for (int i = 0; i<7; i++) {//之前周肯定是七个
                                               isAdd = NO;
                                               for (HXJopRecordListModel *model in result.resultlist) {
                                                   if ([model.Week isEqualToString:self.weekArray[i]]) {//有这天的数据 就添加
                                                       model.isMy = @"no";//不是自己创建的
                                                       [self.dataArray addObject:model];
                                                       [result.resultlist removeObject:model];
                                                       isAdd = YES;
                                                       break;
                                                   }
                                               }
                                               if (isAdd) continue;//添加了这天数据就跳出
                                               //for执行完了没有这天数据
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是周几
                                               NSMutableArray *array = [self lastWeekDate];
                                               myModel.CreateDate = array[i];
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.dataArray addObject:myModel];
                                           }
                                           [self addOtherData];
                                       }else if([result.code isEqualToString:@"400"]){
                                            [self.dataArray removeAllObjects];
                                           for (int i = 0; i<7; i++) {//之前周肯定是七个
                                               HXJopRecordListModel *myModel = [[HXJopRecordListModel alloc]init];
                                               myModel.Week = self.weekArray[i];//告诉是周几
                                               NSMutableArray *array = [self lastWeekDate];
                                               myModel.CreateDate = array[i];
                                               myModel.isMy = @"yes";//是自己创建的
                                               [self.dataArray addObject:myModel];
                                           }
                                           [self addOtherData];
                                       }else{
                                           self.week++;
                                           [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                       }
                                   }
                                   failure:^(NSError *err) {
                                       self.week++;
                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   }];
}

-(void)addNowData{
    [self.nowView removeFromSuperview];
    [self.otherView removeFromSuperview];
    self.otherView = nil;
    self.nowView = nil;
    CGFloat height = 0;
    for (int i = 0;i<self.nowDataArray.count;i++) {
        HXJopRecordListModel  *model = self.nowDataArray[i];
        HXNowJobWeekViewController *vc = self.nowArray[i];
        vc.model = model;
        vc.weekId = [NSString stringWithFormat:@"%ld",(long)_week];
        vc.view.frame = CGRectMake(0, height, self.view.size.width,vc.myHeight);
        height += vc.myHeight;
        [self.nowView addSubview:vc.view];
    }
    self.nowView.frame = CGRectMake(0, 0, self.view.size.width, height);
    [self.scrollView addSubview:self.nowView];
    self.scrollView.contentSize = CGSizeMake(self.view.size.width, height);
}

-(void)addOtherData{
    [self.otherView removeFromSuperview];
    [self.nowView removeFromSuperview];
    self.otherView = nil;
    self.nowView = nil;
    CGFloat height = 0;
    for (int i = 0;i<self.dataArray.count;i++) {
        HXJopRecordListModel  *model = self.dataArray[i];
            HXNowJobWeekViewController *vc = self.nowArray[i];
            vc.model = model;
            vc.weekId = [NSString stringWithFormat:@"%ld",(long)_week];
            vc.view.frame = CGRectMake(0, height, self.view.size.width,vc.myHeight);
            height += vc.myHeight;
            [self.otherView addSubview:vc.view];
    }
    self.otherView.frame = CGRectMake(0, 0, self.view.size.width, height);
    [self.scrollView addSubview:self.otherView];
    self.scrollView.contentSize = CGSizeMake(self.view.size.width, height);
}

/**去看工地*/
-(void)goSiteWithTaskId:(NSString *)str createDate:(NSString *)date{
    HXSitePictureViewController *Vc = [HXSitePictureViewController CreateSitePictureVCWithTaskID:str withIsAdd:NO withIsListOrRiZhiPush:YES withIsExistTaskId:NO withLocalDataDic:@{}];
    [self.navigationController pushViewController:Vc animated:YES];
}

#pragma mark - 时间函数
//今天周几
-(NSInteger)nowWeek{
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute | kCFCalendarUnitWeekday;
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    NSInteger week = [cmp2 weekday];//今天是周几
//    if (week == 1) {
//        week = 8;
//    }
//    week--;
    
    return week;
}

//上一周的日期数组
-(NSMutableArray *)lastWeekDate{
    NSMutableArray *dateArray = [[NSMutableArray alloc]init];
        NSInteger week = [self nowWeek]+(7 * _week);
    for (NSInteger i = week-1; i>[self nowWeek]-1; i--) {
        [dateArray addObject:[self nowWeekDateArray:i]];
    }
    return dateArray;
}


//本周日期数组
-(NSMutableArray *)nowWeekDate{
    NSMutableArray *dateArray = [[NSMutableArray alloc]init];
    NSInteger week = [self nowWeek];
    for (NSInteger i = week-1; i>-1; i--) {
        [dateArray addObject:[self nowWeekDateArray:i]];
    }
    return dateArray;
}

-(NSString *)nowWeekDateArray:(NSInteger)count{
    NSDate*nowDate = [NSDate date];
    NSDate* theDate;
    if(count!=0){
        NSTimeInterval  oneDay = 24*60*60*1; //1天的长度
//        theDate = [nowDate initWithTimeIntervalSinceNow:oneDay*count];
        theDate = [nowDate initWithTimeIntervalSinceNow:-oneDay*count];
    }else{
        theDate = nowDate;
    }
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    return [dateformatter stringFromDate:theDate];
}

//-(void)haha{
//    NSDate* nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
//    NSTimeInterval nowTimeSecond=[nowDate timeIntervalSince1970] * 1000;
//    NSString * nowTimeString = [NSString stringWithFormat:@"%.0f",nowTimeSecond];
//    //将列表时间转化为Date数据
//    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *cellTimeDate = [dateformatter dateFromString:self.creteDate];
//    //将Date数据转化为星期几
//    NSCalendar * myCalendar = [NSCalendar currentCalendar];
//    myCalendar.timeZone = [NSTimeZone systemTimeZone];
//    NSInteger week = [[myCalendar components:NSCalendarUnitWeekday fromDate:cellTimeDate] weekday];
//    NSInteger oneDay = 24*60*60*1;  //1天的长度
//    //将列表时间转化为秒数 将秒转化为毫秒 *1000
//    NSTimeInterval NowSecond=[cellTimeDate timeIntervalSince1970] * 1000;
//    NSString *timeString = [NSString stringWithFormat:@"%.0f", NowSecond];
//}


#pragma mark - 懒加载

- (NSMutableArray<HXJopRecordListModel *> *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (NSMutableArray<HXJopRecordListModel *> *)nowDataArray{
    if (_nowDataArray == nil) {
        _nowDataArray = [[NSMutableArray alloc]init];
    }
    return _nowDataArray;
}

-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        [self.view addSubview:_scrollView];
        _scrollView.frame = CGRectMake(0, 64, self.view.size.width, self.view.size.height - 64);
    }
    return _scrollView;
}

-(UIView *)nowView{
    if (_nowView == nil) {
        _nowView = [[UIView alloc]init];
    }
    return _nowView;
}

-(UIView *)otherView{
    if (_otherView == nil) {
        _otherView = [[UIView alloc]init];
    }
    return _otherView;
}

- (NSArray<HXNowJobWeekViewController *> *)nowArray{
    if (_nowArray == nil) {
        NSMutableArray *arrayM = [[NSMutableArray alloc]initWithCapacity:7];
        for (int i = 0; i<7; i++) {
            HXNowJobWeekViewController *vc = [[HXNowJobWeekViewController alloc]init];
            vc.superVc = self;
            [arrayM addObject:vc];
        }
        _nowArray = [[NSArray alloc]initWithArray:arrayM];
    }
    return _nowArray;
}

- (NSArray<HXJobWeekDetailsViewController *> *)otherArray{
    if (_otherArray == nil) {
        NSMutableArray *arrayM = [[NSMutableArray alloc]initWithCapacity:7];
        for (int i = 0; i<7; i++) {
            HXJobWeekDetailsViewController *vc = [[HXJobWeekDetailsViewController alloc]init];
            vc.superVc = self;
            [arrayM addObject:vc];
        }
        _otherArray = [[NSArray alloc]initWithArray:arrayM];
    }
    return _otherArray;
}

@end
